package com.group7.mall.controller;

import com.group7.mall.bean.Result;
import com.group7.mall.dao.TbSellerDao;
import com.group7.mall.dao.TbStoreAuditFormDao;
import com.group7.mall.dao.TbUserDao;
import com.group7.mall.entity.TbStoreAuditForm;
import com.group7.mall.entity.TbUser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

@RequestMapping("user")
@RestController
public class UserController {

    @Resource
    private TbUserDao tbUserDao;

    @Resource
    private TbSellerDao tbSellerDao;

    @Resource
    private TbStoreAuditFormDao tbStoreAuditFormDao;

    @PostMapping("seller-application")
    public Result<Integer> sellerApplication(@RequestBody TbStoreAuditForm tbStoreAuditForm) {
        if (tbStoreAuditFormDao.queryByUserOpenId(tbStoreAuditForm.getUserOpenId()) != null) {
            return Result.ok(0, "失败！用户已经提交申请");
        }
        if (tbStoreAuditForm.getStoreImage() == null && Objects.equals(tbStoreAuditForm.getStoreImage(), "")) {
            tbStoreAuditForm.setStoreImage("http://117.72.40.8/images/common/22d867b341cdf6f52e2c840b6c7d8cc9.png");
        }
        tbStoreAuditForm.setAuditStatus(0);
        tbStoreAuditForm.setUserOpenId(tbStoreAuditForm.getUserOpenId());
        tbStoreAuditForm.setAuditEndTime("que91");
        TbUser tbUser = tbUserDao.queryByUserOpenId(tbStoreAuditForm.getUserOpenId());
        tbUser.setIdentity(2);
        tbUserDao.update(tbUser);
        System.out.println(tbStoreAuditForm);
        return Result.ok(tbStoreAuditFormDao.insert(tbStoreAuditForm));
    }

    @PostMapping("seller-message-application")
    public Result<Integer> sellerMessageApplication(@RequestBody TbStoreAuditForm tbStoreAuditForm) {
        if (tbStoreAuditFormDao.queryByUserOpenId(tbStoreAuditForm.getUserOpenId()) != null) {
            return Result.ok(0, "失败！用户已经提交申请");
        }
        tbStoreAuditForm.setAuditStatus(0);
        tbStoreAuditForm.setUserOpenId(tbStoreAuditForm.getUserOpenId());
        TbUser tbUser = tbUserDao.queryByUserOpenId(tbStoreAuditForm.getUserOpenId());
        tbUser.setIdentity(2);
        tbUserDao.update(tbUser);
        return Result.ok(tbStoreAuditFormDao.insert(tbStoreAuditForm),"成功！");
    }

    @PostMapping("count-user")
    public Result countUser(@RequestBody TbUser tbUser) {
        return Result.ok(tbUserDao.count(tbUser));
    }

    @PostMapping("add-user")
    public Result addUser(@RequestBody TbUser tbUser) {
        if (tbUserDao.queryByUserOpenId(tbUser.getUserOpenId()) == null) {
            return Result.ok(tbUserDao.insert(tbUser));
        }
        return Result.ok(0);
    }

    @PostMapping("update-user")
    public Result updateUser(@RequestBody TbUser tbUser) {
        tbUser.setId(tbUserDao.queryByUserOpenId(tbUser.getUserOpenId()).getId());
        return Result.ok(tbUserDao.update(tbUser));
    }

    @PostMapping("show-user")
    public Result<TbUser> showUser(@RequestBody TbUser tbUser) {
        return Result.ok(tbUserDao.queryByUserOpenId(tbUser.getUserOpenId()));
    }
}
